
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueue;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    int currentValue=0;
    String pubMSG;



    String pazhanfoStatusMSG="";
    String bianMSG="";

    Map<String,String> serverData;
    List<String> pkRecords;

    controller cn;

    public NettyServerHandler(controller cn){
        pkRecords=new ArrayList<>();

        serverData=new HashMap<>();
        serverData.put("PazhanfoStatus","");
        serverData.put("WumingStatus","");

        this.cn=cn;


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                        cn.setAllStatus(serverData);

                        cn.setPkRecord(pkRecords);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException { // (2)
        String byteBuf = (String) msg;
        pubMSG=byteBuf;
        System.out.println("客户端发来的消息是：" + byteBuf);

        if(byteBuf.contains("writePazhanfoStatus")){
            serverData.put("PazhanfoStatus", LocalDateTime.now()+"PazhanfoStatus "+splitMSG(byteBuf));
        }

        if(byteBuf.contains("writeWumingStatus")){
            serverData.put("WumingStatus",LocalDateTime.now()+" WumingStatus "+splitMSG(byteBuf));
        }

        if(byteBuf.contains("readPazhanfoStatus")){
            ctx.writeAndFlush(Unpooled.copiedBuffer(serverData.get("PazhanfoStatus")+"\r\n", CharsetUtil.UTF_8));
        }

        if(byteBuf.contains("pkRecord")){
            if(pkRecords.size()>=8){
                pkRecords.clear();
            }else{
                pkRecords.add(splitMSG(byteBuf));
            }
        }
    }

    //数据读取完毕事件
    public void channelReadComplete(ChannelHandlerContext ctx) throws IOException, InterruptedException {
        //数据读取完毕，将信息包装成一个Buffer传递给下一个Handler，Unpooled.copiedBuffer会返回一个Buffer
        //调用的是事件处理器的上下文对象的writeAndFlush方法
        //意思就是说将  你好  传递给了下一个handler
        ctx.writeAndFlush(Unpooled.copiedBuffer("服务端已经读取消息完成!"+pubMSG+"\r\n", CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }

    public String splitMSG(String msg){
        return msg.split("@@")[1];
    }



}
